package com.cisystem.system.service.impl;

import java.util.List;

import com.cisystem.common.exception.ServiceException;
import com.cisystem.common.utils.SecurityUtils;
import com.cisystem.common.utils.StringUtils;
import com.cisystem.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cisystem.system.mapper.GoodCatchMapper;
import com.cisystem.system.domain.GoodCatch;
import com.cisystem.system.service.IGoodCatchService;

import javax.validation.Validator;

/**
 * GoodCatchService业务层处理
 *
 * @author kelly
 * @date 2023-07-08
 */
@Service

public class GoodCatchServiceImpl implements IGoodCatchService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private GoodCatchMapper goodCatchMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询GoodCatch
     *
     * @param id GoodCatch主键
     * @return GoodCatch
     */
    @Override
    public GoodCatch selectGoodCatchById(Long id) {
        return goodCatchMapper.selectGoodCatchById(id);
    }

    /**
     * 查询GoodCatch列表
     *
     * @param goodCatch GoodCatch
     * @return GoodCatch
     */
    @Override
    public List<GoodCatch> selectGoodCatchList(GoodCatch goodCatch) {
        return goodCatchMapper.selectGoodCatchList(goodCatch);
    }

    /**
     * 新增GoodCatch
     *
     * @param goodCatch GoodCatch
     * @return 结果
     */
    @Override
    public int insertGoodCatch(GoodCatch goodCatch) {
        return goodCatchMapper.insertGoodCatch(goodCatch);
    }

    /**
     * 修改GoodCatch
     *
     * @param goodCatch GoodCatch
     * @return 结果
     */
    @Override
    public int updateGoodCatch(GoodCatch goodCatch) {
        return goodCatchMapper.updateGoodCatch(goodCatch);
    }

    /**
     * 批量删除GoodCatch
     *
     * @param ids 需要删除的GoodCatch主键
     * @return 结果
     */
    @Override
    public int deleteGoodCatchByIds(Long[] ids) {
        return goodCatchMapper.deleteGoodCatchByIds(ids);
    }

    @Override
    public int reClosedGoodCatchByIds(Long[] ids) {
        return  goodCatchMapper.reClosedGoodCatchByIds(ids);
    }

    /**
     * 删除GoodCatch信息
     *
     * @param id GoodCatch主键
     * @return 结果
     */
    @Override
    public int deleteGoodCatchById(Long id) {
        return goodCatchMapper.deleteGoodCatchById(id);
    }

    @Override
    public String importGoodCatch(List<GoodCatch> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GoodCatch goodCatch : userList) {
            try {
                // 验证是否存在这个用户
                GoodCatch u = goodCatchMapper.selectGoodCatchById(goodCatch.getId());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, goodCatch);
                    goodCatch.setCreateBy(operName);
                    goodCatchMapper.insertGoodCatch(goodCatch);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "ID:" + goodCatch.getId() + ",Title:" + goodCatch.getTitle() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, goodCatch);
                    goodCatch.setUpdateBy(operName);
                    goodCatchMapper.updateGoodCatch(goodCatch);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("ID:").append(goodCatch.getId()).append(",Title:").append(goodCatch.getTitle()).append(" 更新成功");
                } else {
                    failureNum++;
                    successMsg.append("<br/>").append(successNum).append("ID:").append(goodCatch.getId()).append(",Title:").append(goodCatch.getTitle()).append(" 已经存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + successNum + "ID:" + goodCatch.getId() + ",Title:" + goodCatch.getTitle() + " 导入成功";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
